import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/get_rx.dart';

///优惠券控制器
class ShopCardController extends BaseController {

  List dataList = [];
  var page=1.obs;

  bool hasNextPage = false;

  TextEditingController codetxtController = TextEditingController();

  @override
  void onInit() {
    getMyStoreVoucher();
    super.onInit();
  }

  //获取可领取优惠券列表
  Future getMyStoreVoucher() async {
    Tools.showLoading();
  return  Network.getMyStoreVoucher().then((value) {
      print(value);
      dataList = value;
      update();
      Tools.hideLoading();
    }).catchError((error){
      Tools.hideLoading();
    });
  }


  ///领券
  Future storeExchange() async{

    if(codetxtController.text==''){
      Tools.showToast("请输入兑换码");
      return;
    }
    // print(couponId);
    Tools.showLoading();
    Network.storeExchange(codetxtController.text).then((value){
      if(value==true){
        Tools.showToast("兑换成功");
        getMyStoreVoucher();//重新加载
        Tools.hideLoading();
      }
    }).catchError((error){
      print("兑换发生了错误:$error");
      Tools.hideLoading();
    });
  }

  @override
  void onClose() {
    
    super.onClose();
  }

  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    await getMyStoreVoucher();
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading()async {
    super.onPageLoading();
    if(hasNextPage){
      page.value+=1;
      await getMyStoreVoucher();
      baseRefreshCtr.loadComplete();
    }
  }
}
